import type { Router } from "express"
import type { Ioracle, IcallbackFn } from "../../types"

import fs from "fs"

export default (router: Router, _oracle: Ioracle, _callbackFn: IcallbackFn) => {
  router.post("/getData", (_res, req) => {
    fs.readFile("./db/db.json", "utf-8", (err, data) => {
      if (err) {
        req.send({ code: 0, message: err })
      } else {
        req.send({ code: 200, message: "success", data: JSON.parse(data) })
      }
    })
  })

  router.post("/addData", (res, req) => {
    const { content } = res.body
    fs.writeFile("./db/db.json", JSON.stringify(content), (err) => {
      if (err) {
        req.send({ code: 100, message: err })
      } else {
        // file written successfully
        req.send({ code: 200, message: "success" })
      }
    })
  })

  return router
}
